package cn.jhz.learn.blog.service.security.permission.impl;

import cn.jhz.learn.blog.common.exception.ParamException;
import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.dao.account.AccountUserMapper;
import cn.jhz.learn.blog.dao.account.AccountUserRoleMapper;
import cn.jhz.learn.blog.po.account.AccountUser;
import cn.jhz.learn.blog.po.account.AccountUserRole;
import cn.jhz.learn.blog.service.security.permission.UserService;
import cn.jhz.learn.blog.util.JwtUtil;
import cn.jhz.learn.blog.vo.securiity.Login;
import cn.jhz.learn.blog.vo.securiity.Register;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.jose4j.lang.JoseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

@Service("userService")
public class UserServiceImpl implements UserService {

    private final AccountUserMapper userMapper;
    private final AccountUserRoleMapper userRoleMapper;

    @Autowired
    public UserServiceImpl(AccountUserMapper userMapper, AccountUserRoleMapper userRoleMapper) {
        this.userMapper = userMapper;
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public void addOnlineUser(Login userInfo, HttpServletResponse reponse) throws JoseException {
        UsernamePasswordToken userToken = new UsernamePasswordToken(userInfo.getLoginId(), userInfo.getPassword());
        Subject subject = SecurityUtils.getSubject();

        subject.login(userToken);
//        AccountUser user = (AccountUser) subject.getPrincipal();

        reponse.setHeader(
                GLOBAL.TOKEN_INFO.AUTH_HEADER,
                JwtUtil.getInstance().produceJwsToken(
                        JwtUtil.getInstance().createJwtClaims(
                                userInfo.getLoginId(),
                                "online",
                                null,
                                null
                        )
                )
        );
        reponse.setHeader(GLOBAL.HEADER_INFO.ACCESS_CONTROL_EXPOSE_HEADERS, GLOBAL.TOKEN_INFO.AUTH_HEADER);
    }

    public void addUser(Register userInfo){
        /* 1:判断loginId是否已存在 */
        /* 2:判断email是否已存在 */
        /* 3:将密码改为密文 */
        /* 4:创建用户 */
        /* 5:创建用户角色关联 */

        if(userMapper.countLoginId(userInfo.getLoginId()) > 0)
            throw new ParamException("账号已被使用");
        if(userMapper.countEmail(userInfo.getEmail()) > 0)
            throw new ParamException("邮箱已被注册");

        AccountUser user = AccountUser.builder()
                                .loginId(userInfo.getLoginId())
                                .email(userInfo.getEmail())
                                .password(new Sha256Hash(userInfo.getPassword(), ByteSource.Util.bytes(userInfo.getEmail()), 3).toBase64())
                                .status((byte)1)
                                .build();

        userMapper.insertSelective(user);

        AccountUserRole relation = AccountUserRole.builder()
                                .accountUserId(user.getId())
                                .accountRoleId(1)
                                .build();

        userRoleMapper.insert(relation);
    }

    @Override
    public AccountUser getUserByLoginId(String loginId) {
        return userMapper.selectByLoginId(loginId);
    }
}
